package com.ymx.darling.spring.service.impl;

import com.ymx.darling.bean.entity.BeanInfo;
import com.ymx.darling.bean.entity.BeanInfoManager;
import com.ymx.darling.bean.entity.FieldInfo;
import com.ymx.darling.dialect.page.Page;
import com.ymx.darling.mapper.EntityMapper;
import com.ymx.darling.session.setting.QuerySetting;
import com.ymx.darling.spring.service.IService;
import com.ymx.darling.util.ReflectUtil;
import com.ymx.darling.wrapper.QueryWrapper;
import com.ymx.darling.wrapper.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author 兴哥的小新
 * @param <M> 自定义Mapper
 * @param <T> 实体类型
 */
public class ServiceImpl<M extends EntityMapper<T>, T> implements IService<T> {
    @Autowired
    private M entityMapper;

    @Override
    public M getEntityMapper() {
        return this.entityMapper;
    }

    @Override
    public List<T> getAll() {
        return entityMapper.selectList(null);
    }

    @Override
    public List<T> getList(QueryWrapper queryWrapper) {
        return entityMapper.selectList(queryWrapper);
    }

    @Override
    public <E> List<E> getListAs(QueryWrapper queryWrapper, Class<E> asType) {
        return entityMapper.selectListAs(queryWrapper, asType);
    }

    @Override
    public T getById(Serializable id) {
        return entityMapper.selectById(id);
    }

    @Override
    public List<T> getByIds(List<? extends Serializable> ids) {
        return entityMapper.selectBatchIds(ids);
    }

    @Override
    public T getOne(QueryWrapper queryWrapper) {
        return entityMapper.selectOne(queryWrapper);
    }

    @Override
    public <E> E getOneAs(QueryWrapper queryWrapper, Class<E> asType) {
        return entityMapper.selectOneAs(queryWrapper, asType);
    }

    @Override
    public Integer getCount(QueryWrapper queryWrapper) {
        return entityMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer getCount() {
        return entityMapper.selectCount(null);
    }

    @Override
    public Page<T> getPage(QueryWrapper queryWrapper, int pageNum, int pageSize) {
        QuerySetting.setPage(pageNum, pageSize);
        return entityMapper.selectPage(queryWrapper);
    }

    @Override
    public boolean save(T entity) {
        return entityMapper.insert(entity) == 1;
    }

    @Override
    public boolean saveOrUpdate(T entity) {
        Objects.requireNonNull(entity, "保存或更新的实体不能为null");
        BeanInfo beanInfo = BeanInfoManager.getBeanInfo(entity.getClass());
        FieldInfo idColumnInfo = beanInfo.getIdColumnInfo();
        Objects.requireNonNull(idColumnInfo, "保存或更新失败,实体"+entity.getClass()+"缺少主键字段");
        Serializable idValue = ReflectUtil.getValue(idColumnInfo.getField(), entity);
        return Objects.nonNull(idValue) && Objects.nonNull(this.getById(idValue)) ? this.update(entity) : this.save(entity);
    }

    @Override
    public boolean update(T entity) {
        return entityMapper.update(entity) == 1;
    }

    @Override
    public boolean updateAll(T entity) {
        return entityMapper.updateAll(entity) == 1;
    }

    @Override
    public boolean removeById(Serializable id) {
        return entityMapper.deleteById(id) == 1;
    }

    @Override
    public boolean removeByWrapper(UpdateWrapper wrapper) {
        return entityMapper.delete(wrapper) > 0;
    }

    @Override
    public boolean removeByIds(List<? extends Serializable> ids) {
        return entityMapper.deleteBatchIds(ids) > 0;
    }
}
